package it.koutifaris.travianmap.mapsqlparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

import junit.framework.Assert;
import android.test.AndroidTestCase;

public class MapSqlParserTest extends AndroidTestCase {

	private MapSqlParser parser;
	private InputStream testMapSource;

	public void setUp() throws Exception {
		testMapSource = new GZIPInputStream(getClass().getResourceAsStream("map.sql.gz"));
		parser = new MapSqlParser();
	}

	public void test() throws Exception {
		String line = "INSERT INTO `x_world` VALUES (754,353,400,2,120516,'-R2-Susina',10852,'nathan',1347,'SOLI',25);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(754);
		expected.setX(353);
		expected.setY(400);
		expected.setTribeId(2);
		expected.setVillageId(120516);
		expected.setVillageName("-R2-Susina");
		expected.setPlayerId(10852);
		expected.setPlayerName("nathan");
		expected.setAllianceId(1347);
		expected.setAllianceName("SOLI");
		expected.setVillagePopulation(25);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithEmpty() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA',5293,'JULIUS',0,'',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA");
		expected.setPlayerId(5293);
		expected.setPlayerName("JULIUS");
		expected.setAllianceId(0);
		expected.setAllianceName("");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithQuotes() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA',5293,'JUL''IUS',0,'',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA");
		expected.setPlayerId(5293);
		expected.setPlayerName("JUL'IUS");
		expected.setAllianceId(0);
		expected.setAllianceName("");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithEndingQuotes() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA''',5293,'JULIUS',0,'',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA'");
		expected.setPlayerId(5293);
		expected.setPlayerName("JULIUS");
		expected.setAllianceId(0);
		expected.setAllianceName("");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithStartingQuotes() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA',5293,'''JULIUS',0,'Ally',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA");
		expected.setPlayerId(5293);
		expected.setPlayerName("'JULIUS");
		expected.setAllianceId(0);
		expected.setAllianceName("Ally");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testParseAll() throws IOException, Exception {
		parser.parse(testMapSource, new MapSqlEntryHandler() {

			@Override
			public boolean onEntry(MapSqlEntry actual) {
				int expected = (400 + actual.getX()) + 801 * (400 - actual.getY()) + 1;
				Assert.assertEquals(expected, actual.getId());

				return true;
			}
		});
	}
}
